$(function () {
    let now = 'p1'//默认p1先手
    let p1Set = new Set([])//p1棋子
    let p2Set = new Set([])//p2棋子

    let r1 = new Set([1, 2, 3])
    let r2 = new Set([4, 5, 6])
    let r3 = new Set([7, 8, 9])
    let r4 = new Set([1, 4, 7])
    let r5 = new Set([2, 5, 8])
    let r6 = new Set([3, 6, 9])
    let r7 = new Set([1, 5, 9])
    let r8 = new Set([3, 5, 7])
    let winnerSets = new Set([
        r1, r2, r3, r4, r5, r6, r7, r8
    ])

    //判断是否结束
    function checkEnd(playerSet) {
        let end = false
        winnerSets.forEach(item => {
            if (hasSet(item, playerSet)) {
                end = true
            }
        });
        return end
    }

    //playerSet符合某个胜利的条件
    function hasSet(ruleSet, playerSet) {
        let result = true
        ruleSet.forEach(item => {
            if (!playerSet.has(item)) {
                result = false
            }
        });
        return result
    }


    $('.block').click(function () {
        console.log({ now, p1Set, p2Set });
        let mark = parseInt($(this).attr('mark'))
        if (now == 'p1') {
            p1Set.add(mark)
            $(this).css('background-color', 'red')
            if (checkEnd(p1Set)) {
                alert('winner is p1')
            }
            now = "p2"
        }
        if (now == 'p2') {
            p2Set.add(mark)
            $(this).css('background-color', 'green')
            if (checkEnd(p2Set)) {
                alert('winner is p2')
            }
            now = 'p1'
        }
    })

})